<template>
  <div class="product">
    <div class="product-left">
      <div class="product-left-title">产品分类</div>
      <div class="product-left-search">
        <el-input
          placeholder="搜索产品"
          suffix-icon="el-icon-search"
          v-model="searchProduct"
          @keyup.enter.native="getProductData"
        >
        </el-input>
      </div>
      <div class="product-left-menu">
        <el-menu default-active="1" unique-opened class="el-menu-vertical-demo">
          <el-submenu
            :index="classify.name"
            v-for="(classify, classIdx) in classifyData"
            :key="classIdx"
          >
            <template slot="title">
              <span>{{ classify.name }}</span>
            </template>
            <el-menu-item-group
              v-for="(classifyChild, ChildIdx) in classify.children"
              :key="ChildIdx"
            >
              <template v-if="classifyChild.children">
                <el-submenu :index="classifyChild.name">
                  <template slot="title">{{ classifyChild.name }}</template>

                  <el-menu-item
                    :index="children.name"
                    v-for="(children, index) in classifyChild.children"
                    :key="index"
                    @click="onChildrenProduct(children)"
                    >{{ children.name }}</el-menu-item
                  >
                </el-submenu>
              </template>
              <template v-else>
                <el-menu-item :index="classifyChild.name">{{
                  classifyChild.name
                }}</el-menu-item>
              </template>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </div>
    </div>
    <div class="product-right">
      <div class="product-right-title">产品市场</div>
      <div class="product-right-content">
        <ProductGoods :list="productData" />
      </div>
      <div class="product-right-pagination">
        <el-pagination
          @size-change="productSizeChange"
          @current-change="productCurrentChange"
          :current-page="page"
          :page-sizes="[15, 20, 25, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="productTotal"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import ProductGoods from '@/components/product-goods';
import Product from '@/api/product';
import Way from '@/utils/way';

export default {
  name: 'product',
  components: { ProductGoods },
  props: {},
  data() {
    return {
      searchProduct: '', // 搜索产品
      page: 1, // 当前页数
      pageSize: 15, // 每页数量
      productData: [], // 产品列表
      productTotal: 0, // 数量
      classifyData: [], // 分类列表
      cateId: '', // 分类id
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getProductData();
    /**
     * 如果有数据的或者时间小于1天的话
     * 就不能请求数据
     */
    const newData = new Date().getDate();
    const errData = localStorage.getItem('beforeTime');
    const productClassify = localStorage.getItem('productClassify');
    console.log(newData - errData >= 1);
    (newData - errData >= 1 || !productClassify) &&
      this.getProductClassifyList();
    this.classifyData = productClassify && JSON.parse(productClassify);
  },
  methods: {
    // 获取产品列表
    getProductData() {
      const that = this;
      const param = {
        cateId: that.cateId,
        title: that.searchProduct,
        page: that.page,
        limit: that.pageSize,
      };
      Product.getProductList(param).then((res) => {
        console.log(res);
        if (res.code == 200) {
          that.productData = res.data.rows;
          that.productTotal = res.data.total;
        }
      });
    },
    // 获取产品分类列表
    getProductClassifyList() {
      const that = this;
      Product.getProductClassifyList({ id: 0 }).then((res) => {
        if (res.code == 200) {
          that.classifyData = res.data;
          localStorage.setItem('productClassify', JSON.stringify(res.data));
          localStorage.setItem('beforeTime', new Date().getDate());
        } else {
          Way.setMessage(res.msg, 3);
        }
      });
    },
    // 分类点击事件
    onChildrenProduct(item) {
      this.cateId = item.id;
      this.getProductData();
      console.log(item);
    },
    // 分页当前页数
    productSizeChange(val) {
      this.pageSize = val;
      this.getProductData();
    },
    // 分页每页数量
    productCurrentChange(val) {
      this.page = val;
      this.getProductData();
    },
  },
  mounted() {},
};
</script>

<style scoped lang="less">
.product {
  display: flex;
  height: 100%;

  .product-left {
    display: flex;
    flex-direction: column;
    margin-right: 24px;
    width: 180px;

    .product-left-title {
      height: 66px;
      line-height: 66px;
      font-size: 16px;
      font-weight: 700;
      color: rgba(0, 0, 0, 0.85);
      border-bottom: 2px solid rgba(201, 201, 201, 0.5);
    }

    .product-left-search {
      margin-top: 24px;
    }

    .product-left-menu {
      flex: 1;

      .el-menu-vertical-demo {
        overflow-y: auto;
        height: 720px;

        // 取消滚动条
        &::-webkit-scrollbar {
          width: 0;
          height: 0;
          color: transparent;
        }
      }

      /deep/ .el-menu {
        border-right: unset;
      }
      /deep/ .el-submenu .el-menu-item {
        min-width: unset;
      }
    }
  }

  .product-right {
    flex: 1;

    .product-right-title {
      height: 66px;
      line-height: 66px;
      font-size: 16px;
      font-weight: 700;
      color: rgba(0, 0, 0, 0.85);
      border-bottom: 2px solid rgba(201, 201, 201, 0.5);
    }

    .product-right-content {
      margin-top: 24px;
    }

    .product-right-pagination {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 80px;
    }
  }
}
</style>
